----------------------------------------------------------------------------------------------------------------------------------
--[[


]]--
----------------------------------------------------------------------------------------------------------------------------------
--
    local function GetReplica(self)
        return self.inst.replica.hutao_com_cosmic_hat_sys_for_item or self.inst.replica._.hutao_com_cosmic_hat_sys_for_item
    end
    local function set_player(self, player)
        local replica = GetReplica(self)
        if replica then
            replica:LinkPlayer(player)
        end
    end
    local function set_current(self,value)
        local replica = GetReplica(self)
        if replica then
            replica:SetCurrent(value)
        end
    end
    local function set_max(self,value)
        local replica = GetReplica(self)
        if replica then
            replica:SetMax(value)
        end
    end
----------------------------------------------------------------------------------------------------------------------------------
local hutao_com_cosmic_hat_sys_for_item = Class(function(self, inst)
    self.inst = inst

    self.DataTable = {}
    self.TempTable = {}
    self._onload_fns = {}
    self._onsave_fns = {}


    ------------------------------------------------------------
    ---
        self.player = nil
        inst:DoTaskInTime(0,function()
            if self.player == nil then
                inst:Remove()
                print("乾坤帽 未绑定玩家，已删除")
            else
                --- 监听玩家移除事件，避免bug
                inst:ListenForEvent("onremove",function()
                    inst:Remove()
                end,self.player)
            end
        end)
    ------------------------------------------------------------
    --- bad luck value
        self.current = 0
        self.max = 777.7
        self:AddOnLoadFn(function()
            self.current = self:Get("current",0)
            self.max = self:Get("max",777.7)
        end)
        self:AddOnSaveFn(function()
            self:Set("current",self.current)
            self:Set("max",self.max)
        end)
    ------------------------------------------------------------
end,
nil,
{
    player = set_player,
    current = set_current,
    max = set_max,
})
------------------------------------------------------------------------------------------------------------------------------
-- current / max
    function hutao_com_cosmic_hat_sys_for_item:DoDelta(value)
        self:SetCurrent(self.current + value)
    end
    function hutao_com_cosmic_hat_sys_for_item:SetCurrent(value)
        local old = self.current
        self.current = math.clamp(value, 0, self.max)
        self.inst:PushEvent("onbadluckchange",{
            old = old,
            new = self.current
        })
        if old < self.current and self.current == self.max then
            self.inst:PushEvent("onbadluckfull")
        elseif old > self.current and self.current == 0 then
            self.inst:PushEvent("onbadluckempty")
        end
    end
    function hutao_com_cosmic_hat_sys_for_item:GetCurrent()
        return self.current
    end
    function hutao_com_cosmic_hat_sys_for_item:SetMax(value)
        self.max = value
    end
    function hutao_com_cosmic_hat_sys_for_item:GetMax()
        return self.max
    end
------------------------------------------------------------------------------------------------------------------------------
-- 内部物品使用倍率
    function hutao_com_cosmic_hat_sys_for_item:SetInnerUseMult(value)
        self.use_mult = value
    end
    function hutao_com_cosmic_hat_sys_for_item:GetInnerUseMult()
        return self.use_mult or 1
    end
------------------------------------------------------------------------------------------------------------------------------
-- 链接玩家
    function hutao_com_cosmic_hat_sys_for_item:LinkPlayer(player)
        self.player = player
        if player and self.inst.components.named then
            -- local hat_display_name = self.inst:GetDisplayName()
            local hat_display_name = tostring(STRINGS.NAMES[string.upper(self.inst.prefab)])
            local player_name = player:GetDisplayName()
            self.inst.components.named:SetName(hat_display_name .. " ( " .. player_name .. " )")
        end
    end
    function hutao_com_cosmic_hat_sys_for_item:GetLinkedPlayer()
        return self.player
    end
    function hutao_com_cosmic_hat_sys_for_item:IsLinkingPlayer(player)
        return self.player == player
    end
    function hutao_com_cosmic_hat_sys_for_item:GetOwner()
        return self.player
    end
------------------------------------------------------------------------------------------------------------------------------
-- 
------------------------------------------------------------------------------------------------------------------------------
----- onload/onsave 函数
    function hutao_com_cosmic_hat_sys_for_item:AddOnLoadFn(fn)
        if type(fn) == "function" then
            table.insert(self._onload_fns, fn)
        end
    end
    function hutao_com_cosmic_hat_sys_for_item:ActiveOnLoadFns()
        for k, temp_fn in pairs(self._onload_fns) do
            temp_fn(self)
        end
    end
    function hutao_com_cosmic_hat_sys_for_item:AddOnSaveFn(fn)
        if type(fn) == "function" then
            table.insert(self._onsave_fns, fn)
        end
    end
    function hutao_com_cosmic_hat_sys_for_item:ActiveOnSaveFns()
        for k, temp_fn in pairs(self._onsave_fns) do
            temp_fn(self)
        end
    end
------------------------------------------------------------------------------------------------------------------------------
----- 数据读取/储存

    function hutao_com_cosmic_hat_sys_for_item:Get(index,default)
        if index then
            return self.DataTable[index] or default
        end
        return nil or default
    end
    function hutao_com_cosmic_hat_sys_for_item:Set(index,theData)
        if index then
            self.DataTable[index] = theData
        end
    end

    function hutao_com_cosmic_hat_sys_for_item:Add(index,num,min,max)
        if index then
            if min and max then
                local ret = (self.DataTable[index] or 0) + ( num or 0 )
                ret = math.clamp(ret,min,max)
                self.DataTable[index] = ret
                return ret
            else
                self.DataTable[index] = (self.DataTable[index] or 0) + ( num or 0 )
                return self.DataTable[index]
            end
        end
        return 0
    end
------------------------------------------------------------------------------------------------------------------------------

------------------------------------------------------------------------------------------------------------------------------
    function hutao_com_cosmic_hat_sys_for_item:OnSave()
        self:ActiveOnSaveFns()
        local data =
        {
            DataTable = self.DataTable
        }
        return next(data) ~= nil and data or nil
    end

    function hutao_com_cosmic_hat_sys_for_item:OnLoad(data)
        if data.DataTable then
            self.DataTable = data.DataTable
        end
        self:ActiveOnLoadFns()
    end
------------------------------------------------------------------------------------------------------------------------------
return hutao_com_cosmic_hat_sys_for_item







